<template>
  <div class="imageselect-wrap">
    <el-radio-group
      v-if="!item?.multiple"
      @change="handleChange"
      v-model="changeValue"
    >
      <template
        v-for="option in item?.config.options || []"
        :key="option?.value"
      >
        <div
          class="img-radio-item"
          :class="{ 'is-checked': isChecked(option.value) }"
          v-if="isShowOption(`${item?.vModel}_${option?.value}`)"
          @click="handleSelect(option.value)"
        >
          <el-image
            :preview-src-list="[option.image]"
            :src="option.image"
            class="image"
            fit="scale-down"
          >
            <template #error>
              <div class="image-slot">
                <el-icon>
                  <ele-Picture />
                </el-icon>
              </div>
            </template>
          </el-image>
          <vote
            :value="option.value"
            :vote-list="voteList"
            :total-vote="totalVote"
            v-if="item?.config.showVoteResult"
          ></vote>
          <div class="checkbox-slot">
            <el-radio :value="option.value">
              <div>
                {{ option.label }}
              </div>
            </el-radio>
          </div>
        </div>
      </template>
    </el-radio-group>
    <el-checkbox-group
      v-else
      @change="handleChange"
      v-model="changeValue"
      class="img-select-checkbox-group"
    >
      <template
        v-for="option in item?.config.options"
        :key="option.value"
      >
        <div
          class="img-radio-item"
          :class="{ 'is-checked': isChecked(option.value) }"
          v-if="isShowOption(`${item?.vModel}_${option.value}`)"
          @click="handleSelect(option.value)"
        >
          <vote
            :value="option.value"
            :type="'img'"
            :vote-list="voteList"
            :total-vote="totalVote"
            v-if="item.config.showVoteResult"
          ></vote>
          <el-image
            :preview-src-list="[option.image]"
            :src="option.image"
            class="image"
            fit="cover"
          >
            <template #error>
              <div class="image-slot">
                <el-icon>
                  <ele-Picture />
                </el-icon>
              </div>
            </template>
          </el-image>
          <div class="checkbox-slot">
            <el-checkbox :value="option.value">
              <div>
                {{ option.label }}
              </div>
            </el-checkbox>
          </div>
        </div>
      </template>
    </el-checkbox-group>
  </div>
</template>

<script setup name="TImageSelect" lang="ts">
import Vote from "../TVote/vote.vue";
import { watch } from "vue";
import { useFormItemExam } from "../hooks/useExamHook";
import { formEmits, formItemProps, useFormItem } from "../hooks/useFormItemHook";
import { BasicComponent } from "@/views/formgen/components/GenerateForm/types/form";
import { useVoteHook } from "@/views/formgen/components/FormItem/TVote/useVoteHook";
import { useUserFormOptionLogicInject } from "../../GenerateForm/hooks/useUserFormInject";

const props = defineProps({
  ...formItemProps
});

const emits = defineEmits([...formEmits, "changeLabel"]);

useFormItemExam(<BasicComponent>props.item);
const formItemHook = useFormItem(props, emits);
const { changeValue, getOptions } = formItemHook;
const voteHook = useVoteHook(props);
const { handleVote, handleArrayVote, voteList, totalVote } = voteHook;
const { isShowOption, initWatchCheckRule } = useUserFormOptionLogicInject();

const handleSelect = (value: string | number) => {
  if (props.item?.multiple) {
    const currentValue = Array.isArray(changeValue.value) ? [...changeValue.value] : [];
    const index = currentValue.indexOf(value);
    if (index > -1) {
      currentValue.splice(index, 1);
    } else {
      currentValue.push(value);
    }
    changeValue.value = currentValue;
    handleArrayVote(currentValue);
  } else {
    changeValue.value = value;
    handleVote(value);
  }
};

watch(
  () => changeValue,
  (val: any) => {
    if (!changeValue.value) {
      return;
    }
    let labelValue = null;
    if (Array.isArray(val)) {
      labelValue = getOptions()
        .filter((item: any) => changeValue.value.includes(item.value))
        .map((item: any) => item.label);
    } else {
      labelValue = getOptions().find((item: any) => changeValue.value == item.value)?.label;
    }
    emits("changeLabel", labelValue);
  },
  {
    deep: true
  }
);

// 自动选中
initWatchCheckRule(props.item?.vModel as string, (val: any[]) => {
  if (props.item?.multiple) {
    if (changeValue.value) {
      changeValue.value.push(...val);
    } else {
      changeValue.value = val;
    }
  } else {
    changeValue.value = val[0];
  }
  handleChange(changeValue.value);
});

const handleChange = (val: any) => {
  if (Array.isArray(val)) {
    handleArrayVote(changeValue.value);
  } else {
    handleVote(changeValue.value);
  }
};

const isChecked = (value: string | number) => {
  if (Array.isArray(changeValue.value)) {
    return changeValue.value.includes(value);
  } else {
    return changeValue.value === value;
  }
};
</script>

<style lang="scss" scoped>
.imageselect-wrap {
  width: 100%;
  overflow-x: auto;
  padding: 12px 0;
}

:deep(.el-radio-button__inner, .el-radio-group, .el-checkbox-group) {
  display: flex !important;
  flex-wrap: wrap;
  width: 100%;
  gap: 12px;
  padding: 0 8px;
}

.img-radio-item {
  border-radius: 10px;
  display: flex;
  flex-direction: column !important;
  align-content: center !important;
  align-items: center !important;
  border: 1px solid rgba(177, 177, 177, 0.1) !important;
  background-color: #fff;
  margin: 5px;
  width: calc(25% - 12px);
  min-width: 200px;
  cursor: pointer;
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  &.is-checked {
    border: 2px solid var(--el-color-primary) !important;
    background-color: var(--el-color-primary-light-9);
  }

  .image {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    padding: 8px;
  }
}

.checkbox-slot {
  width: 100%;
  padding: 0 12px 12px;

  :deep(.el-checkbox, .el-radio) {
    width: 100%;
    display: flex;
    align-items: flex-start;
    white-space: normal !important;
    word-break: break-word;
    padding: 8px 0;

    .el-checkbox__input,
    .el-radio__input {
      flex-shrink: 0;
      margin-right: 8px;
      margin-top: 2px;
    }

    .el-checkbox__label,
    .el-radio__label {
      flex: 1;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      line-height: 1.2;
      padding-right: 8px;
      white-space: normal !important;
      word-break: break-word;
      position: relative;

      &:hover {
        .view-more {
          opacity: 1;
        }
      }

      .view-more {
        position: absolute;
        right: 0;
        bottom: 0;
        background: rgba(255, 255, 255, 0.9);
        padding: 0 4px;
        color: #409EFF;
        cursor: pointer;
        opacity: 0;
        transition: opacity 0.3s;
        font-size: 12px;
      }
    }
  }
}

:deep(.el-radio__label, .el-checkbox__label) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  white-space: normal !important;
  word-break: break-word;
  line-height: 1.2;
}

.img-select-checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 0 8px;
}

:deep(.image-slot) {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 24px;
}

.detail-dialog {
  :deep(.el-dialog__body) {
    padding: 20px;
    max-height: 60vh;
    overflow-y: auto;
  }
}

@media (max-width: 1200px) {
  .img-radio-item {
    width: calc(33.33% - 12px);
  }
}

@media (max-width: 992px) {
  .img-radio-item {
    width: calc(50% - 12px);
  }
}

@media (max-width: 768px) {
  .img-radio-item {
    width: calc(50% - 12px);
    min-width: unset;
  }
}

@media (max-width: 576px) {
  .img-radio-item {
    width: calc(50% - 12px);
    min-width: unset;
  }
  
  .checkbox-slot {
    padding: 0 8px 8px;
  }
}
</style>
